<template>
  <div class="contentCate">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input
          v-model="searchParams.userId"
          placeholder="请输入用户id"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-input
          v-model="searchParams.luckDrawId"
          placeholder="请输入幸运屋id"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-input
          v-model="searchParams.code"
          placeholder="请输入随机码"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-select
          v-model="searchParams.status"
          size="mini"
          clearable
          placeholder="请选择状态"
        >
          <el-option
            v-for="item in statusData"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData(1)"
          v-if="has('luckDrawCodes/index')"
        >搜索</el-button
        >
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          @click="reset"
          v-if="has('luckDrawCodes/index')"
        >重置</el-button
        >
      </el-row>
      <el-table
        :data="data.data"
        v-loading="table_loading"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          prop="code"
          label="随机码"
          align="center"
        ></el-table-column>
        <el-table-column label="用户信息" align="center" width="150px">
          <template slot-scope="scope">
            <div>
              <el-image
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
                fit="contain"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px',
                  alignItems: 'center'
                }"
              ></el-image>
              <div
                style="display: flex;flex-direction: column;justify-content: space-between;"
              >
                <span>{{ scope.row.nickname }}</span>
                <span>ID:{{ scope.row.user_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="幸运屋信息" align="center" width="160px">
          <template slot-scope="scope">
            <div style="align-items: center;">
              <el-image
                :src="scope.row.box_image"
                :preview-src-list="[scope.row.box_image]"
                :style="{
                  width: '50px',
                  height: '50px',
                  borderRadius: '5px',
                  marginRight: '10px'
                }"
              ></el-image>
              <div>
                <div>{{ scope.row.box_name }}</div>
                <span>ID:{{ scope.row.box_id }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="is_finish" label="状态" align="center">
          <template slot-scope="scope">
            <span :style="jinduArr[scope.row.status - 1].style"><b>{{ jinduArr[scope.row.status - 1].title }}</b></span>
          </template>
        </el-table-column>
        <el-table-column
          prop="cy_at"
          label="参与时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="kj_at"
          label="开奖时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="created_at"
          label="创建时间"
          align="center"
        ></el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";
export default {
  name: "contentCate",
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      pageSize: 20,
      searchParams: {
        page: 1,
        limit: 10,
        level: "",
        userId: "",
        status: "",
        startTime: "",
        endTime: ""
      },
      levelData: [],
      jinduArr: [
        {id: 0, title: "待使用", style: {color: "red"}},
        {id: 1, title: "已使用", style: {color: "green"}}
      ],
      statusData: [
        { id: 1, name: "待使用" },
        { id: 2, name: "已使用" },
      ]
    };
  },
  created() {
    this.getData(0);
    this.getLevel();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData(flag) {
      this.table_loading = true;
      if (flag) {
        this.searchParams.page = 1;
      }
      api.luckDrawCodes
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getLevel() {
      api.common
        .goodsLevels()
        .then(res => {
          if (res.data.code == 200) {
            this.levelData = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData(0);
    },
    // 初始化
    init() {
      this.searchParams.level = "";
      this.searchParams.page = 1;
      this.searchParams.userId = "";
      this.searchParams.status = "";
      this.searchParams.type = "";
      this.searchParams.startTime = "";
      this.searchParams.endTime = "";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData(0);
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData(0);
    },
    // 删除
    del(id, index) {
      this.$confirm(
        "此操作将删除该内容分类及其下的所有内容, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          closeOnClickModal: false,
          type: "warning"
        }
      )
        .then(() => {
          api.blindBoxOrderPrizes
            .del(id)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!"
                });
                this.data.data.splice(index, 1);
                this.data.total--;
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="less">
.contentCate {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
